package com.kunya.product.service.impl.active;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.google.common.collect.Lists;
import com.kunya.common.common.ServerResponse;
import com.kunya.common.util.PropertiesUtil;
import com.kunya.product.dao.active.ProductDiscountMapper;
import com.kunya.product.po.Product;
import com.kunya.product.po.ProductDiscount;
import com.kunya.product.service.active.IProductDiscountService;
import com.kunya.product.vo.ProductDiscountVo;
import com.kunya.product.vo.ProductListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fuzhili
 * @Description: 折扣商品实现层
 * @date 2018/7/14 13:57
 */
@Service("iproductDiscountService")
public class ProductDiscountServiceImpl  implements IProductDiscountService {
    @Autowired
    private ProductDiscountMapper productDiscountMapper;

   @Override
    public ServerResponse<List<ProductDiscountVo>> getProductAllByShowInShelveAndName(String productName, String tablePrefix) {
        if(StringUtil.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorMessage("查询商品，缺失参数");
        }
        StringBuilder builder=new StringBuilder();
        builder.append("%");
        builder.append(productName);
        builder.append("%");
        List<ProductDiscountVo> list=productDiscountMapper.getProductAllByShowInShelveAndName(builder.toString(),tablePrefix);
        if(list==null){
            return ServerResponse.createByErrorMessage("保存商品折扣，失败");
        }
        return ServerResponse.createBySuccess("查询商品，成功",list);
    }

    @Override
    public ServerResponse saveProductDiscount(ProductDiscount productDiscount, String tablePrefix) {
        if(productDiscount==null||StringUtil.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorMessage("保存折扣商品，缺失参数");
        }
        int returnCode=productDiscountMapper.insertSelective(productDiscount,tablePrefix);
        if(returnCode<=0){
            return ServerResponse.createByErrorMessage("保存商品折扣，失败");
        }
        return ServerResponse.createBySuccessMessage("保存商品折扣，成功");
    }

    @Override
    public ServerResponse<ProductDiscountVo> getProductDiscountByProductId(Long productDiscountId, String tablePrefix) {
        if(productDiscountId==null||StringUtil.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorMessage("查询折扣商品，缺失参数");
        }
        ProductDiscountVo productDiscountVo=productDiscountMapper.getProductDiscountByProductId(productDiscountId,tablePrefix);
        if(productDiscountVo==null){
            return ServerResponse.createByErrorMessage("查询指定折扣商品失败，或没有改折扣商品");
        }
        return ServerResponse.createBySuccess("查询指定折扣商品，成功",productDiscountVo);
    }

    @Override
    public ServerResponse<ProductDiscount> updateProductDiscount(ProductDiscount productDiscount, String tablePrefix) {
        if(productDiscount==null||StringUtil.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorMessage("修改折扣商品，缺失参数");
        }
        int returnCode=productDiscountMapper.updateByPrimaryKeySelective(productDiscount,tablePrefix);
        if(returnCode<=0){
            return  ServerResponse.createByErrorMessage("修改折扣商品，失败");
        }
        return ServerResponse.createBySuccessMessage("修改折扣商品，成功");
    }

    @Override
    public ServerResponse deleteProductDiscount(Long productDiscountId, String tablePrefix) {
        if(productDiscountId==null||StringUtil.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorMessage("删除折扣商品，缺失参数");
        }
        int returnCode=productDiscountMapper.deleteByPrimaryKey(productDiscountId,tablePrefix);
        if(returnCode<=0){
            return ServerResponse.createByErrorMessage("删除折扣商品，失败");
        }
        return ServerResponse.createBySuccessMessage("删除折扣商品，成功");
    }

    @Override
    public ServerResponse<PageInfo> getProductDiscountAll(int pageNum, int pageSize, String tablePrefix) {
        PageHelper.startPage(pageNum,pageSize);
        if(StringUtil.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorMessage("后台查询折扣商品，缺失参数");
        }
        List<ProductDiscountVo> list=productDiscountMapper.getProductDiscountAll(tablePrefix);
        if(list==null){
            return ServerResponse.createByErrorMessage("后台查询折扣商品，失败");
        }
        PageInfo pageInfo=new PageInfo(list);
        return ServerResponse.createBySuccess("后台查询折扣商品，成功",pageInfo);
    }

    @Override
    public ServerResponse<PageInfo> getProductDiscountAllByFront(int pageNum, int pageSize, String tablePrefix) {
        PageHelper.startPage(pageNum,pageSize);
        if(StringUtil.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorMessage("前台查询折扣商品，缺失参数");
        }

        List<Product> list=productDiscountMapper.getProductDiscountAllByFront(tablePrefix);
        List<ProductListVo>  productVoList = Lists.newArrayList();
        for (Product product : list) {
            ProductListVo productListVo = assembleProductListVo(product);
            productVoList.add(productListVo);
        }
        PageInfo pageInfo=new PageInfo(list);
        return ServerResponse.createBySuccess("查询热门商品，成功",pageInfo);
    }
    //将product对象转化为list使用的vo对象
    private ProductListVo assembleProductListVo(Product product) {
        ProductListVo productListVo = new ProductListVo();
        BeanUtils.copyProperties(product,productListVo);
//        productListVo.setCategoryId(product.getCategoryId());
        //图片主机地址从配置文件中读取，不存数据库，为了迁移灵活
        productListVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));
        return productListVo;
    }
}
